package com.ddtech.framework.notice.service.impl;

import com.ddtech.framework.notice.model.SysNotice;
import com.ddtech.framework.notice.repository.SysNoticeRepository;
import com.ddtech.framework.notice.service.SysNoticeService;
import com.ddtech.framework.notice.service.dto.SysNoticeDTO;
import com.ddtech.framework.notice.service.dto.SysNoticeQueryCriteria;
import com.ddtech.framework.notice.service.mapper.SysNoticeMapper;
import com.ddtech.framework.notice.service.mapper.SysNoticeVoMapper;
import com.ddtech.framework.system.repository.UserRepository;
import com.ddtech.utils.PageUtil;
import com.ddtech.utils.QueryHelp;
import com.ddtech.utils.SecurityUtils;
import com.ddtech.utils.ValidationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
* @author bin_pu
* @date 2019-11-27
*/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SysNoticeServiceImpl implements SysNoticeService {

    @Autowired
    private SysNoticeRepository sysNoticeRepository;

    @Autowired
    private SysNoticeMapper sysNoticeMapper;
    @Autowired
    private SysNoticeVoMapper sysNoticeVoMapper;

    @Autowired
    private UserRepository userRepository;

    @Override
    public Object queryAll(SysNoticeQueryCriteria criteria, Pageable pageable){
        Page<SysNotice> page = sysNoticeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(sysNoticeVoMapper::toDto));
    }

    @Override
    public Object queryAll(SysNoticeQueryCriteria criteria){
        return sysNoticeMapper.toDto(sysNoticeRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public SysNoticeDTO findById(String id) {
        Optional<SysNotice> sysNotice = sysNoticeRepository.findById(id);
        ValidationUtil.isNull(sysNotice,"SysNotice","id",id);
        return sysNoticeMapper.toDto(sysNotice.get());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysNoticeDTO create(SysNotice resources) {
        resources.setOperator(userRepository.findByUsername(SecurityUtils.getUsername()));
        return sysNoticeMapper.toDto(sysNoticeRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysNotice resources) {
        Optional<SysNotice> optionalSysNotice = sysNoticeRepository.findById(resources.getId());
        ValidationUtil.isNull( optionalSysNotice,"SysNotice","id",resources.getId());
        SysNotice sysNotice = optionalSysNotice.get();
        sysNotice.copy(resources);
        sysNoticeRepository.save(sysNotice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        sysNoticeRepository.deleteById(id);
    }
}